package com.fy.oa.zc.log;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.dom4j.DocumentException;

import com.fy.oa.zc.exception.FyOAException;
import com.fy.oa.zc.rest.FyOARestClient;
import com.seeyon.apps.collaboration.event.AbstractCollaborationEvent;

public class FyOAFormEventHandlerLogger {
	private static final Log logger = LogFactory.getLog(FyOAFormEventHandlerLogger.class);
	private FyOARestClient fyOARestClient;
	private FyOAFileLogger fyOAFileLogger;

	public void setFyOAFileLogger(FyOAFileLogger fyOAFileLogger) {
		this.fyOAFileLogger = fyOAFileLogger;
	}

	public void setFyOARestClient(FyOARestClient fyOARestClient) {
		this.fyOARestClient = fyOARestClient;
	}

	/**
	 * 用于记录触发的事件信息
	 * 
	 * @param joinpoint
	 *            {@JoinPoint}
	 */
	public void beforHandlerLog(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		com.seeyon.apps.collaboration.event.AbstractCollaborationEvent e = (AbstractCollaborationEvent) joinpoint
				.getArgs()[0];
		StringBuilder sb = new StringBuilder().append("beforHandlerLog ").append(methodName).append(" 表单：")
				.append(e.getClass().getSimpleName()).append(",summaryid:").append(e.getSummaryId());
		pringLog(sb.toString());
	}

	/**
	 * 用于记录返回内容
	 * 
	 * @param joinpoint
	 *            {@JoinPoint}
	 * @param returnValue
	 *            {@String}
	 */
	public void afterReturningHandlerLog(JoinPoint joinpoint, Object returnValue) {
		String methodName = joinpoint.getSignature().getName();
		com.seeyon.apps.collaboration.event.AbstractCollaborationEvent e = (AbstractCollaborationEvent) joinpoint
				.getArgs()[0];
		StringBuilder sb = new StringBuilder().append("afterHandlerLog ").append(methodName).append(",summaryid:")
				.append(e.getSummaryId()).append("\nreturnValue:").append(returnValue);
		pringLog(sb.toString());
	}

	/**
	 * 记录处理异常事件
	 * 
	 * @param joinpoint
	 * @param e
	 */
	public void afterThrowExceptionLog(JoinPoint joinpoint, FyOAException e) {
		String methodName = joinpoint.getSignature().getName();
		com.seeyon.apps.collaboration.event.AbstractCollaborationEvent event = (AbstractCollaborationEvent) joinpoint
				.getArgs()[0];
		StringBuilder sb = new StringBuilder().append("处理").append(methodName).append("事件发生异常，异常原因：")
				.append(e.getMessage()).append("\n异常表单ID：").append(event.getSummaryId());
		String msg = sb.toString();
		boolean success = false;
		try {
			if (!fyOARestClient.formImport(msg, "002218", event.getSummaryId())) {
				success = true;
			}
		} catch (DocumentException e2) {
			pringLog("写入异常日志信息表发生异常", e2);
		}
		try {
			if (!success) {
				fyOAFileLogger.writeMessage(msg, e);
			}
		} catch (IOException e1) {
			pringLog("写入日志文件发生异常", e1);
		}
		pringLog(msg, e);
	}

	/**
	 * 打印进入的方法
	 * 
	 * @param joinpoint
	 *            {@JoinPoint}
	 */
	public void beforManagerLog(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		StringBuilder sb = new StringBuilder().append("beforManager ").append(methodName).append("  ")
				.append(joinpoint.getTarget().toString());
		pringLog(sb.toString());
	}

	private void pringLog(String msg) {
		logger.info(msg);
	}

	private void pringLog(String msg, Exception e) {
		logger.error(msg, e);
		System.out.println(msg);
	}
}
